//
//  Kylin_ClearCacheCell.m
//  03-BuDeJie-2 TabBar设置
//
//  Created by Kylin on 16/2/19.
//  Copyright © 2016年 kylin. All rights reserved.
//

#import "Kylin_ClearCacheCell.h"
#import <SVProgressHUD.h>

#define Kylin_CachePath  [[NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES)firstObject] stringByAppendingPathComponent:@"default"]
static NSString * const Kylin_CearCellText = @"清除缓存";
@implementation Kylin_ClearCacheCell


- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    
    if (self = [super initWithStyle:style reuseIdentifier:  reuseIdentifier]) {
        
        self.textLabel.text = Kylin_CearCellText;
        
        // 禁止占击事件
        self.userInteractionEnabled = NO;
        
        // 右边的显示圈圈
        UIActivityIndicatorView *loadView = [[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
        // 开始旋转
        [loadView startAnimating];
        
        self.accessoryView = loadView;
        
        
        // 计算缓存大小
        [[[NSOperationQueue alloc]init]addOperationWithBlock:^{
       
            NSInteger size = Kylin_CachePath.fileSize;
            
            CGFloat unit = 1000.0;
            
            NSString *sizeText = nil;
            
            if (size >= unit * unit * unit ) {// GB
                
                sizeText = [NSString stringWithFormat:@"%.1fGB",size / unit / unit / unit];
            }else if (size >= unit * unit ){//MB
                sizeText = [NSString stringWithFormat:@"%.1fMB",size / unit / unit];
            }else if (size >= unit ){//KB
                sizeText = [NSString stringWithFormat:@"%.1fKB",size / unit ];
            }else {
                sizeText = [NSString stringWithFormat:@"%zdB",size];

            }
            
            // 获取最终的文字
            NSString *text = [NSString stringWithFormat:@"%@(%@)",Kylin_CearCellText,sizeText];
            
            // 回到主线程
            [[NSOperationQueue mainQueue]addOperationWithBlock:^{
                self.textLabel.text = text;
                self.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
                self.accessoryView = nil;
                
                // 允许点击事件
                self.userInteractionEnabled = YES;
        }];
        
        
        }];
    }
    
    return self;
}

- (void)updateStatus{
    
    // 如果 不是转菊花,就直接返回
    if (self.accessoryView == nil) return;
    
    // 让转圈圈继续
    UIActivityIndicatorView *loadView = (UIActivityIndicatorView *)self.accessoryView;
    [loadView startAnimating];
}

- (void)clearCaches{
    
    [SVProgressHUD showWithStatus:@"正在清除" maskType:SVProgressHUDMaskTypeBlack];
    [[[NSOperationQueue alloc]init] addOperationWithBlock:^{
        [[NSFileManager defaultManager] removeItemAtPath:Kylin_CachePath error:nil];
       [[NSOperationQueue mainQueue] addOperationWithBlock:^{
           [SVProgressHUD showSuccessWithStatus:@"清除成功"];
           
           self.textLabel.text = Kylin_CearCellText;
           
           // 清除缓存时禁止点击
           self.userInteractionEnabled = NO;
       }];
    }];
}
@end
